package com.xlhj.rabbit.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @Author: caijliu
 * @Date: 04/09/2021 15:45
 * @Description TODO
 * @Vsersion: 0.0.1
 */
@RestController
@RequestMapping("/delayed")
public class DelayedProducerController {

    private static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
    private static final String DELAYED_ROUT_KEY = "delayed.routingkey";
    private static Logger logger = LoggerFactory.getLogger(DelayedProducerController.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("sendMessage/{message}/{delayTime}")
    public void sendMessage(@PathVariable String message, @PathVariable Integer delayTime) {
        String uuid = UUID.randomUUID().toString();
        CorrelationData correlationData = new CorrelationData(uuid);
        rabbitTemplate.convertAndSend(DELAYED_EXCHANGE_NAME, DELAYED_ROUT_KEY, message, processor -> {
            processor.getMessageProperties().setDelay(delayTime);
            return processor;
        }, correlationData);
        logger.info("发送一条延时：{}毫秒的消息：{}给队列delayed.queue", delayTime, message);
    }
}
